package org.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Timer;
import java.util.TimerTask;

// 主菜单状态实现 - 修改为符合第一个界面要求
public abstract class MainMenuState implements GameState {
    private final CodeRebirthGame game;
    private JButton startButton, loadButton, exitButton;
    private static final int WIDTH = 1024;
    private static final int HEIGHT = 768;
    private int gradientPos = 0;
    private Timer gradientTimer;

    public MainMenuState(CodeRebirthGame game) {
        this.game = game;
    }

    @Override
    public void enter() {
        game.setLayout(new BorderLayout());

        JPanel mainPanel = new JPanel(new BorderLayout()) {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                render(g);
            }
        };
        mainPanel.setOpaque(false);


        // 修改按钮面板布局
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
        buttonPanel.setOpaque(false);
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 50, 50, 0));

        startButton = createMenuButton("开始游戏", e -> game.changeState(new ImageState1(game) {
            @Override
            public void render() {

            }
        }));
        loadButton = createMenuButton("加载存档", e -> showMessage("存档功能将在后续版本实现"));
        exitButton = createMenuButton("退出游戏", e -> System.exit(0));

        buttonPanel.add(Box.createVerticalGlue());
        buttonPanel.add(startButton);
        buttonPanel.add(Box.createVerticalStrut(20));
        buttonPanel.add(loadButton);
        buttonPanel.add(Box.createVerticalStrut(20));
        buttonPanel.add(exitButton);

        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
        game.add(mainPanel);

        // 添加鼠标点击监听
        mainPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                game.changeState(new ImageState1(game) {
                    @Override
                    public void render() {

                    }
                });
            }
        });

        // 启动渐变动画
        gradientTimer = new Timer();
        gradientTimer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                gradientPos = (gradientPos + 2) % (WIDTH + 200);
                game.repaint();
            }
        }, 0, 30);
    }

    private JButton createMenuButton(String text, ActionListener listener) {
        JButton button = new JButton(text);
        button.setAlignmentX(Component.LEFT_ALIGNMENT);
        button.setPreferredSize(new Dimension(200, 50));
        button.setFont(game.getResourceManager().getFont("title", 24f));
        button.setBackground(new Color(70, 130, 180));
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.addActionListener(listener);
        return button;
    }

    private void showMessage(String msg) {
        JOptionPane.showMessageDialog(game, msg, "提示", JOptionPane.INFORMATION_MESSAGE);
    }

    @Override
    public void render(Graphics g) {
        // 深色背景
        g.setColor(new Color(10, 10, 20));
        g.fillRect(0, 0, WIDTH, HEIGHT);

        // 动态渐变标题
        String title = "代码重生";
        g.setFont(game.getResourceManager().getFont("title", 72f));

        // 创建渐变效果
        for (int i = 0; i < title.length(); i++) {
            int x = WIDTH/2 - g.getFontMetrics().stringWidth(title)/2 + g.getFontMetrics().charWidth(title.charAt(0))*i;
            int pos = gradientPos - x;
            if (pos > 0 && pos < 200) {
                float ratio = pos / 200f;
                g.setColor(new Color((int)(ratio*50), (int)(ratio*200), 255));
            } else if (pos >= 200) {
                g.setColor(Color.BLACK);
            } else {
                g.setColor(Color.BLACK);
            }
            g.drawString(String.valueOf(title.charAt(i)), x, 200);
        }
    }

    @Override public void update() {}

    @Override
    public void exit() {
        if (gradientTimer != null) {
            gradientTimer.cancel();
        }
        game.getContentPane().removeAll();
    }

    @Override public void handleInput(KeyEvent e) {}

    public abstract void render();
}
